package com.computaris.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.computaris.ws.Event;
import com.computaris.ws.EventImpl;

public class SearchData extends HttpServlet {
	private static final long serialVersionUID = 2519736864739668156L;
	
	private static final int ALL_TYPES = 4;
	private static final String[] callTypes = new String[] {"Voice", "Sms", "Mms", "Data"};
	private static final String[] uoms = new String[] {"Sec", "Num", "Kb"};
	
	Logger log = Logger.getLogger(SearchData.class.getSimpleName());

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
		res.setContentType("text/html");
		String msisdn = req.getAttribute("msisdn").toString();
		
		res.getWriter().write("Your msisdn is " + msisdn);
	}
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
		res.setContentType("text/html");
		PrintWriter writer = res.getWriter();
		
		String msisdn = req.getParameter("msisdn");
		String dateFrom = req.getParameter("dateFrom");
		String dateTo = req.getParameter("dateTo");
		int callType = Integer.parseInt(req.getParameter("callType"));
		
		log.info("received message " + msisdn + " - " + dateFrom + " - " + dateTo + " - " + callType);
		EventImpl impl = new EventImpl();
		
		List<Event> events = msisdn.trim().length() == 0 ? impl.getAllEvents() : impl.getEvents(msisdn);
		if(events == null) {
			writer.write("Connection to database failed");
			return;
		}
		
		writer.write("<table id=\"myTable\" class=\"tablesorter\"><thead><tr><th>MSISDN</th><th>Date</th><th>Call Type</th><th>Cost</th><th>Amount</th><th>UOM</th></tr></thead><tbody>");
		
		DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
		Date df = null, dt = null;
		try {
			df = formatter.parse(dateFrom);
			dt = formatter.parse(dateTo);
		}catch (Exception e) {
			writer.write("Invalid format for dates");
			return;
		}

		for(Event event : events) {
			if(event.getType() != callType && callType != ALL_TYPES) {
				if(callType != 3 || event.getType() != 2) {
					continue;
				}
			}
			if(callType == ALL_TYPES && event.getType() == 3) {
				continue;
			}
			Date date = event.getEventDate();
			if(date.before(df) || date.after(dt)) {
				continue;
			}
			
			writer.write("<tr><td>" + 
					event.getSubsriberId() + "</td><td>" + 
					event.getEventDate() + "</td><td>" + 
					callTypes[event.getType()] + "</td><td>" + 
					event.getCharge() + "</td><td>" + 
					event.getAmount() + "</td><td>" + 
					uoms[event.getUom()] + "</td></tr>");
		}

		writer.write("</tbody></table>");
	}
}
